package com.hluobo.gnnulibrary.ui;

import com.hluobo.gnnulibrary.R;
import com.hluobo.gnnulibrary.adapter.BookListAdapter;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SearchFragment extends Fragment {

	private static final String TAG = "com.hluobo.gnnulibrary";
	
	private View view; 
	private ListView bookList;
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		EditText text = (EditText) view.findViewById(R.id.search_et_search);
		
		text.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(actionId == EditorInfo.IME_ACTION_SEARCH){
					Log.i(TAG, "search for: "  + v.getText());  
					// TODO search
					searchBooks(v.getText().toString());
				}
				
				return false;
			}
			
		});
		
		Button searchAdvance = (Button) view.findViewById(R.id.search_bt_advance);
		searchAdvance.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i(TAG, "searchAdvance clicked");
				loadSearchAdvanceActivity();
			}
		});
		
		bookList = (ListView) view.findViewById(R.id.search_lv_list);
//		TextView emptyView = new TextView(getActivity());
//		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//		emptyView.setLayoutParams(params);
//		emptyView.setGravity(Gravity.CENTER);
//		emptyView.setText("当前无搜索结果…");
//		bookList.setEmptyView(emptyView);
		bookList.setDivider(null);
		bookList.setAdapter(new BookListAdapter(getActivity()));
	}
	
	private void loadSearchAdvanceActivity(){
		Intent intent = new Intent(getActivity(), SearchAdvanceActivity.class);
		getActivity().startActivity(intent);
	}
	
	private void searchBooks(String conditon){
		// TODO 搜索
	}



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(view == null)
			view = inflater.inflate(R.layout.fragment_search, container, false);
		return view;
	}

}
